;
(function (call) {
    var main = {
        userInfo: null,
        mapId: "jcybMap",
        init: function () {
            this.initView();
            this.bindEvt();
        },
        initView: function () {
            var userInfo = $("#userInfo").data('info');
            this.userInfo = userInfo;
            var time = $("#datepicker .datepicker-date span").text()
            var endTime = new Date(time).format("yyyy-MM-dd hh:mm");
            var startTime = new Date(endTime);
            startTime.setHours(startTime.getHours() - 24);
            startTime = startTime.format("yyyy-MM-dd hh:mm");
            this.processStartTime = CafeJS.Datepicker({
                appendTo: "#processStartTime",
                type: 1,
                dateFmt: "yyyy-MM-dd hh:mm",
                curDate: startTime
            });
            this.processEndTime = CafeJS.Datepicker({
                appendTo: "#processEndTime",
                type: 1,
                dateFmt: "yyyy-MM-dd hh:mm",
                curDate: endTime
            });
            this.getAreaData();
        },
        // 获取区域数据
        getAreaData: function () {
            var that = this;
            var $select = $("#addProcessUnit");
            $.getJSON(api.internal.business.getAreaData, function (res) {
                if (res.isSuccess) {
                    var data = res.data;
                    for (var i = 0; i < data.length; i++) {
                        if (Number(that.userInfo.userAreaCode) === data[i].areaId) {
                            $select.append(`<option selected value="${data[i].areaId}">${data[i].areaName}</option>`);
                        } else {
                            $select.append(`<option value="${data[i].areaId}">${data[i].areaName}</option>`);
                        }
                    }
                }
            });
        },
        // 保存事件
        saveEvent: function () {
            var that = this;
            var startTime = $("#processStartTime .datepicker span").text();
            var endTime = $("#processEndTime .datepicker span").text();
            var name = $("#eventName").val();
            var areaCode = $("#addProcessUnit").val();
            if (!name) {
                alert('事件名称不能为空');
                return;
            }
            var params = {
                startTime: new Date(startTime).format('yyyy-MM-dd hh:mm:00'),
                endTime: new Date(endTime).format('yyyy-MM-dd hh:mm:00'),
                name: name,
                userId: that.userInfo.smuser.iid,
                areaCode: areaCode
            }
            $.ajax({
                url: api.internal.business.addEventCfg,
                type: "get",
                dataType: 'json',
                data: params,
                success: function (res) {
                    if (res.isSuccess) {
                        alert("保存成功");
                        $("#modal_add_process .close").trigger('click');
                        window.processMonitor.queryEventAll();
                    } else {
                        alert("保存失败");
                    }
                }
            })
        },
        bindEvt: function () {
            var that = this;
            $(".add-process-btn #sureBtn").click(function () {
                that.saveEvent();
            })
            $(".add-process-btn #cancelBtn").click(function () {
                $("#modal_add_process .close").trigger('click');
            })
        },
    };
    if (typeof call === 'function') {
        call(main);
    }
})(function (main) {
    CafeJS.models(['datepicker'], function () {
        main.init();
    });
});